package com.tdzx.ui;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.tdzx.R;
import com.tdzx.constant.Constant;
import com.tdzx.entity.Member;
import com.tdzx.util.JsonParser;
import com.tdzx.util.SharePrefenceUtil;

/** 
 * @author utopia
 * @version 2013-9-9 下午3:44:56
 * 
 */
public class ClosePeople extends BaseActivity_NoBar {
	AsyncHttpClient client;
	UILApplication application;
	String lat;
	String lon;
	
	ImageLoader imageLoader = ImageLoader.getInstance();
	
	List<Member> listClose;
	CloseAdapter cAdapter;
	ListView closeList;
	
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == UILApplication.location_cuccess) {
				lat = application.locData.latitude + "";
				lon = application.locData.longitude + "";
			}
			super.handleMessage(msg);
		}
		
	};
	
	int page = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.close_people);
		initViewTop();
		client = new AsyncHttpClient();
		application = (UILApplication) getApplication();
		if(application.locData != null) {
			lat = application.locData.latitude + "";
			lon = application.locData.longitude + "";
		} else {
			application.startRequest(handler);
		}
		
		cAdapter = new CloseAdapter();
		closeList = (ListView) findViewById(R.id.closeList);
		closeList.setAdapter(cAdapter);
		closeList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent_other = new Intent(ClosePeople.this, OtherPeopleInfo.class);
				intent_other.putExtra("id", listClose.get(position).getMember_Id());
				startActivity(intent_other);
			}
		});
		
		showProgress("正在加载数据...");
		getData();
		
	}
	
	 ImageView caterory_back;
	 ImageView cat_src;
	private void initViewTop() {
		caterory_back = (ImageView) findViewById(R.id.caterory_back);
		caterory_back.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				Log.i("ccc", "onTouch ca");
				cat_src.onTouchEvent(arg1);
				return false;
			}
		});
		
		caterory_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		cat_src = (ImageView) findViewById(R.id.cat_src);
		cat_src.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.i("ccc", "onTouch crc");
				return true;
			}
		});
	}

	public void getData() {
		client.get(Constant.getLocationMember + "lat=" + lat + "&lon=" + lon + "&page=" + page + "&pageSize=10&memberId=" + SharePrefenceUtil.getUserID(ClosePeople.this), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				finishProgress();
				listClose = JsonParser.getMemberList(content);
				cAdapter.notifyDataSetChanged();
				super.onSuccess(content);
			}

			@Override
			public void onFailure(Throwable error, String content) {
				finishProgress();
				showToast("网络错误，获取数据失败");
				super.onFailure(error, content);
			}
			
		});
	}
	
	class CloseAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return listClose == null ? 0 : listClose.size();
		}

		@Override
		public Object getItem(int arg0) {
			return listClose.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			if(view == null) {
				view = View.inflate(ClosePeople.this, R.layout.close_item, null);
			}
			final ImageView sns_msg_avatar_iv = (ImageView) view.findViewById(R.id.sns_msg_avatar_iv);
			TextView sns_msg_nick_tv = (TextView) view.findViewById(R.id.sns_msg_nick_tv);
			TextView sns_msg_content_tv = (TextView) view.findViewById(R.id.sns_msg_content_tv);
			TextView sns_msg_qm = (TextView) view.findViewById(R.id.sns_msg_qm);
			
			ImageView user_sex1 = (ImageView) view.findViewById(R.id.user_sex1);
			
			sns_msg_nick_tv.setText(listClose.get(position).getMember_Nick_Name());
			double distance = 0;
			try {
				distance = Double.valueOf(listClose.get(position).getDistance());
			} catch (Exception e){
				
			}
			sns_msg_content_tv.setText((int)distance + "米以內");
			if(listClose.get(position).getMember_Info() != null && !listClose.get(position).getMember_Info().equals("null")) {
				sns_msg_qm.setText(listClose.get(position).getMember_Info());
			}
			
			if(!listClose.get(position).getMemeber_Sex().equals("null")) {
				if(listClose.get(position).getMemeber_Sex().equals("男")) {
					user_sex1.setImageResource(R.drawable.ic_user_male2);
					user_sex1.setVisibility(View.VISIBLE);
				} else if(listClose.get(position).getMemeber_Sex().equals("女")) {
					user_sex1.setImageResource(R.drawable.ic_user_famale2);
					user_sex1.setVisibility(View.VISIBLE);
				} else {
					user_sex1.setVisibility(View.GONE);
				}
			} else {
				user_sex1.setVisibility(View.GONE);
			}
			
			if(listClose.get(position).getMember_Avatar() == null || listClose.get(position).getMember_Avatar().equals("null") || listClose.get(position).getMember_Avatar().equals("")) {
//				img.setVisibility(View.GONE);
				sns_msg_avatar_iv.setBackgroundResource(R.drawable.mini_avatar_shadow);
			} else {
				imageLoader.displayImage(listClose.get(position).getMember_Avatar(),
						sns_msg_avatar_iv,new ImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
					}
					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						sns_msg_avatar_iv.setImageBitmap(null);
						sns_msg_avatar_iv.setBackgroundResource(R.drawable.mini_avatar_shadow);
					}
					@Override
					public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
						sns_msg_avatar_iv.setImageBitmap(null);
						sns_msg_avatar_iv.setBackgroundDrawable(new BitmapDrawable(loadedImage));
					}
					@Override
					public void onLoadingCancelled(String imageUri, View view) {
					}
				});
			}
			
			return view;
		}
		
	}
}
